package com.jinxin.platform.cdysylives.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.jinxin.platform.cdysylives.bean.bo.AppKeySecretInfo;
import com.jinxin.platform.cdysylives.common.CommonCodeEntity;
import com.jinxin.platform.cdysylives.common.Constant;
import com.jinxin.platform.cdysylives.common.ResultEntity;
import com.jinxin.platform.cdysylives.redis.RedisUtil;
import com.jinxin.platform.cdysylives.service.IAccesstokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class IAccesstokenServiceImpl implements IAccesstokenService {
	@Autowired
	private RedisUtil redisUtil;

	@Override
	public ResultEntity getAkInfoByUser() {
		String jsonStr = (String)redisUtil.get(Constant.YSY_APPKEY_SECRET);
		if(!StringUtils.isEmpty(jsonStr)){
			AppKeySecretInfo appIdSecretInfo = JSONObject.parseObject(jsonStr, AppKeySecretInfo.class);
			return new ResultEntity(CommonCodeEntity.SUC_CODE,"获取配置信息成功",appIdSecretInfo);
		}
		return new ResultEntity(CommonCodeEntity.FAIL,"获取配置信息失败","");
	}
	@Override
	public ResultEntity saveOrUpdateKey(AppKeySecretInfo appIdSecretInfo) {
		if(appIdSecretInfo != null){
			JsonObject json = new JsonObject();
			json.addProperty("appKey",appIdSecretInfo.getAppKey());
			json.addProperty("secret",appIdSecretInfo.getSecret());
			boolean isSucceed = redisUtil.set(Constant.YSY_APPKEY_SECRET, json.toString());
			if(isSucceed){
				return new ResultEntity(CommonCodeEntity.SUC_CODE, "保存成功","" );
			}else {
				return new ResultEntity(CommonCodeEntity.FAIL, "redis异常，保存失败","" );
			}
		}
		return new ResultEntity(CommonCodeEntity.PARAM_ERR, "参数异常，保存失败","" );
	}

}
